package com.example.demo.cbhs;

import com.alibaba.excel.EasyExcel;
import com.example.demo.model.CalculateData;
import com.example.demo.utils.JsonUtil;
import com.example.demo.utils.OpenUtils;
import org.apache.poi.util.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author linjingze
 * @date 2023/12/18 4:55 PM
 */
public class ChengBenHeSuanReader {



    public static void main(String[] args) {
        BigDecimal headBSPrice = new BigDecimal("4.22");
        BigDecimal hsy = new BigDecimal("3500");

        CalculateConfig calculateConfig = new CalculateConfig();
        calculateConfig.setBsHeadPrice(headBSPrice);
        calculateConfig.setUnBsHeadPrice(new BigDecimal("701.39"));
        calculateConfig.setHyfBasePrice(hsy);
        calculateConfig.setArea("纽约");

        String fileName = "/Users/linjingze/Downloads/程乔玉工作/FCLN23120292：成本测算（NYC拆）.xls";
        //String fileName = "/Users/linjingze/Downloads/程乔玉工作/FCLN23120091：成本测算（NYC拆）.xls";
        //fileName = "/Users/linjingze/Downloads/FCLN23120269：成本测算（CHI拆）.xls";
        File file = new File(fileName);
        calculateConfig.setFile(file);
        doCbhs(calculateConfig);
    }

    public static void doCbhs(CalculateConfig calculateConfig) {
        System.out.println(JsonUtil.serialize(calculateConfig));
        File file = calculateConfig.getFile();
        CalculateListener readListener = new CalculateListener(calculateConfig);
        EasyExcel.read(file, readListener).sheet().doRead(); // 读取第一个工作表
        CalculateData calculateData = readListener.getCalculateData();
        String opFileName = file.getParent() + File.separator + "成本核算" + System.currentTimeMillis() + ".xlsx";
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(opFileName);
            FillCalculateTemplate.fill(fileOutputStream, FillCalculateTemplate.class.getResourceAsStream("/成本模版.xlsx"), calculateData);
            OpenUtils.open(opFileName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(fileOutputStream);
        }
    }


}
